package com.nyist.utils;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 1.
 *
 * @ClassName HtmlToJson
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/25/10:54
 */
@Slf4j
public class HtmlToJson2 {

    public Map<String, String> sendGetHtml(String baseUrl, String params) {
        String targetUrl = baseUrl + "?" + params;
        Map<String, String> map = new HashMap<>();
        try {
            Document doc = Jsoup.connect(targetUrl).get();
            log.info("====>>>title: {}",doc.title());
            Elements elementsByClass = doc.getElementsByClass("form-group col-auto");

            Element element1 = elementsByClass.get(0);
            Elements elementsByTag = element1.getElementsByTag("input");

            for (Element element : elementsByTag) {
                // 打印
                // log.info("====>>>element: {}",element);
                String name = element.attr("name");
                String value = element.attr("value");
                // 打印
                log.info("====>>>element===name: {}",name);
                // 打印
                log.info("====>>>element===value: {}",value);
                map.put(name,value);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return map;
    }

    public static void main(String[] args) {
        String baseUrl = "https://cloud.tradelinksig.com/cas/login";
        String params = "service=https://cloud.tradelinksig.com/rocars&locale=zh_CN";

        HtmlToJson2 htmlToJson = new HtmlToJson2();
        Map<String, String> map = htmlToJson.sendGetHtml(baseUrl, params);
        // for (Map.Entry<String, String> entry : map.entrySet()) {
        //     System.out.println(entry);
        // }

    }

}
